package com.sojson.project.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.controller.param.verify.ActivityParamVerify;
import com.sojson.project.entity.po.Activity;
import com.sojson.project.entity.vo.ActivityVo;
import com.sojson.project.service.ActivityService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 活动表控制器
 * 
 * @author liu
 * @date 2021-03-24
 */
@Api(tags = "活动表管理")
@RestController
@RequestMapping(value = "/activity")
public class ActivityController {

    @Resource
    private ActivityService activityServiceImpl;

    /**
     * 单条插入
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    @PostMapping(value = "/insert")
    @PreAuthorize("@ss.hasPermi('/activity/insert')")
    @ApiOperation("添加活动")
    public Result<Integer> insert(@RequestBody Activity activity) throws SQLException {
        // 校验参数
        Result<Integer> paramResult = ActivityParamVerify.insert(activity);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        activity.insertInit();
        return Result.success(activityServiceImpl.insert(activity));
    }

    /**
     * 批量插入
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Result<Integer> insertList(@RequestBody Activity activity) throws SQLException {
        return Result.success(activityServiceImpl.insertList(activity));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Result<Integer> deleteByDto(@RequestBody Activity activity) throws SQLException {
        return Result.success(activityServiceImpl.deleteByDto(activity));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Result<Integer> countAll() throws SQLException {
        int countAll = activityServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Result<List<Activity>> findAll() throws SQLException {
        List<Activity> findAll = activityServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Result<Integer> countByDto(@RequestBody Activity activity) throws SQLException {
        int countByDto = activityServiceImpl.countByDto(activity);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/activity/findByDto')")
    @ApiOperation(value = "根据对象查询活动", response = Activity.class)
    public Object findByDto(Activity activity) throws SQLException {
        List<Activity> findByDto = activityServiceImpl.findByDto(activity);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteById")
    public Object deleteById(Integer id) throws SQLException {
        activityServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(activityServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody Activity activity) throws SQLException {
        activityServiceImpl.updateById(activity);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody Activity activity) throws SQLException {
        return Result.success(activityServiceImpl.updateStaticByIdList(activity));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody Activity activity) throws SQLException {
        return Result.success(activityServiceImpl.updateChangeByIdList(activity));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/activity/updatePartialById')")
    @ApiOperation("通过id更新活动")
    public Object updatePartialById(@RequestBody Activity activity) throws SQLException {
        // 校验参数
        Result<?> paramResult = ActivityParamVerify.updatePartialById(activity);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        activity.updateInit();
        activity.setUpdateNull();
        activityServiceImpl.updatePartialById(activity);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody Activity activity) throws SQLException {
        return Result.success(activityServiceImpl.updateStaticPartialByIdList(activity));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody Activity activity) throws SQLException {
        return Result.success(activityServiceImpl.updateChangePartialByIdList(activity));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping("/findById")
    @ApiOperation(value = "通过id查询活动", response = Activity.class)
    public Object findById(String id) throws SQLException {
        Activity findById = activityServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Activity> findByIdList = activityServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody Activity activity) throws SQLException {
        ArrayList<Integer> findIdByDto = activityServiceImpl.findIdByDto(activity);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        activityServiceImpl.clear();
        return Result.success();
    }

    /**
     * 通过id修改状态
     * 
     * @param activity
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updateStatus")
    @PreAuthorize("@ss.hasPermi('/activity/updateStatus')")
    @ApiOperation("通过id修改活动状态")
    public Object updateStatus(@RequestBody Activity activity) throws SQLException {
        // 校验参数
        Result<?> paramResult = ActivityParamVerify.updateStatus(activity);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Activity act = new Activity();
        act.updateInit();
        act.setId(activity.getId());
        act.setStatus(activity.getStatus());
        activityServiceImpl.updatePartialById(act);
        return Result.success();
    }

    /**
     * 获取当前时间的助力活动
     * 
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/findThisHelp")
    @ApiOperation(value = "获取当前时间的助力活动", response = ActivityVo.class)
    public Object findThisHelp() throws Exception {
        ActivityVo findThisHelp = activityServiceImpl.findThisHelp();
        return Result.success(findThisHelp);
    }

    /**
     * 活动助力
     * 
     * @param help
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/help")
    @PreAuthorize("@ss.login()")
    @ApiOperation(value = "活动助力", response = ActivityVo.class)
    public Object help(String helpCode) throws Exception {
        // 校验参数
        Result<?> paramResult = ActivityParamVerify.help(helpCode);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        int help = activityServiceImpl.help(helpCode);
        return Result.success(help);
    }

    /**
     * 获取当前时间的活动
     * 
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/findThis")
    @ApiOperation(value = "获取当前时间的活动", response = ActivityVo.class)
    public Object findThis(ActivityVo activity) throws Exception {
        List<ActivityVo> findThis = activityServiceImpl.findThis(activity);
        return Result.success(findThis, findThis);
    }

}